The present invention relates to a method and apparatus for handling stored data objects and particularly, but not exclusively, to the handling of finalisation for objects in memory compaction and garbage collection procedures executing in real time in real or virtual memory space of a data processing apparatus.
Garbage collection is the automated reclamation of system memory space after its last use by a programme. A number of examples of garbage collecting techniques are discussed in xe2x80x9cGarbage Collection: Algorithms for Automatic Dynamic Memory Managementxe2x80x9d by R. Jones et al, pub. John Wiley and Sons 1996, ISBN 0-471-94148-4, at pages 1 to 18. Whilst the storage requirements of many computer programs are simple and predictable, with memory allocation and recovery being handled by the programmer or a compiler, there is a trend toward functional languages having more complex patterns of execution such that the lifetimes of particular data structures can no longer be determined prior to run-time and hence automated reclamation of this storage, as the program runs, is essential.
Finalisation is a concept used in Sun Microsystems"" Java(copyright) and other current garbage-collected languages and programming environments, such as Modula-3 and Cedar. Stored data objects may have an associated finaliser which is to be executed after the object nominally becomes available for garbage collection but before the data is collected. The purpose of this feature is to allow an object to clean up any other system resources the object has claimed before it is destroyed. For example, the finaliser for a Java File object would close all the system file handles claimed by the object.
However, as a finaliser is just another of the class of object handling methods, with all the power of other methods, the finaliser procedure can access all data objects accessible from the object being finalised. Therefore, all objects reachable by a finaliser must be explicitly excluded from garbage collection. Furthermore, it is possible for the finaliser method to resurrect any such objects reachable by a finaliser, including the object being finalised itself, by making the object reachable again. Consequently, a garbage collection procedure cannot delete any objects that are reachable from a finalisable object until its finaliser has executed and the reachability of the objects has been re-evaluated. In Java and other languages, the possibility of an object repeatedly resurrecting itself is typically removed by stating that the finaliser for each instance is executed only once. This control on finalisation will be assumed herein.
In PC""s or workstations, the extra processing and memory load to support finalisation is not usually a problem due to the amount of memory typically available in a PC, although the support will, of course, affect the overall efficiency. In low-memory environments such as set-top boxes, however, support for finalisers can cause problems and even a concurrent or incremental garbage collector may have to halt the program until it has executed some or all of the outstanding finalisers and reclaimed any memory used by them.
It is therefore an object of the present invention to provide an incremental garbage collection system which supports finalisable objects whilst minimising the time-to-collection for objects wherever possible.
In accordance with the present invention there is provided a concurrent garbage collection and marking method for traversing data structures formed of data objects linked by identifying pointers in a contiguous heap memory, with garbage collection of data objects classed as deletable, the method comprising the steps of:
a) for a selected root object, traversing the pointers carried thereby to determine the objects linked to the root object; and
b) traversing pointers to determine further objects linked to those identified by the previous step;
wherein step b) is repeated until no further pointers remain to be traversed following which the objects identified therein are classed as marked, and all remaining objects in the heap are classed as unmarked unless already classed as such due to an earlier traversal operation in which case they are classed as deletable, characterised in that some of the heap data objects carry finalisers and some further objects are identified as potentially reachable by finalisers which objects are classed as pending, wherein, at the end of each sweep, those objects classed as marked are reclassed as unmarked, those objects classed as unmarked are reclassed as pending, those objects classed as pending are reclassed as deletable, and those objects already classed as deletable are deleted.
The present invention also provides data processing apparatus comprising a data processor coupled with a random access memory containing a plurality of data objects linked in data structures by identifying pointers and within a heap in a contiguous area of the memory, the apparatus further comprising first additional storage means containing for each heap object an identifier for one of a predetermined set of marking classes, and the processor being configured to effect the following operations on the stored plurality of data objects:
a) for a selected root object, traversing the pointers carried thereby to determine the objects linked to the root object; and
b) traversing pointers therefrom to determine further objects linked to those identified;
wherein the processor repeats operation b) until no further pointers remain to be traversed following which the stored class identifiers for the objects identified therein are set as marked, and for all remaining objects in the heap are set as unmarked unless already set as such due to an earlier traversal operation in which case they are set as deletable, characterised in that some of the heap data objects carry finalisers and some further objects are identified as potentially reachable by finalisers which objects are classed as pending, wherein, at the end of each sweep, the processor is arranged for those objects classed as marked to reclass as unmarked, for those objects classed as unmarked to reclass as pending, for those objects classed as pending to reclass as deletable, and to delete those objects already classed as deletable.
In a preferred embodiment, the object reclassification comprises global reinterpretation of mark state classifiers rather than alteration of any individual classifier: in terms of the apparatus configuration, this means the processor is arranged to effect object mark state reclassification by rotating internally held values for each mark class and consequently the need to alter the stored mark state code (suitably a 2-bit binary code) for each and every stored object is avoided.
By way of a refinement for the handling of finalisers, heap data objects carrying finalisers may suitably include a respective flag which, when set, prevents the object from being reclassed as deletable and/or heap data objects carrying finalisers and classed as pending, when located during traversal, may be explicitly placed in a finalisation queue for execution of their finalisers.
As a further refinement, in the apparatus configuration the processor may be further arranged to track the number of objects with finalisers identified during marking, to compare this with a maintained count of finalisers allocated to the heap and, on identification that the totals match, to delete those objects classed as pending at the same time as deleting those objects classed as deletable. This enables earlier removal of certain classes of garbage objects, leading to a general improvement in efficiency.